<template>
  <div id="main" style="height: 700px"></div>
</template>

<script>
import axios from "@/service/index.js";
import * as echarts from "echarts";

export default {
  data() {
    return {
      list: [],
      myChart: null,
    };
  },
  created() {
    axios.get("/data/simpleData").then((res) => {
      console.log(res);
      if (res.code === "200") {
        this.list = res.data;
        console.log(this.list);
        this.drawChart(this.list);
      }
    });
  },
  mounted() {
    // 基于准备好的dom，初始化echarts实例
    this.myChart = echarts.init(document.getElementById("main"));
  },
  methods: {
    drawChart(list) {
      // 绘制图表
      this.myChart.setOption({
        title: {
          text: "ECharts 入门示例",
        },
        legend: {
          top: "bottom", // 底部颜色显示
        },
        tooltip: {},
        toolbox: {
          show: true,
          feature: {
            // 特点
            dataZoom: {
              yAxisIndex: "none",
            },
            mark: { show: true }, // 显示标记
            dataView: { show: true, readOnly: false }, // 查看文本数据
            magicType: { type: ["line", "bar"] },
            restore: { show: true }, // 可刷新 恢复、还原原始图表
            saveAsImage: { show: true }, // 显示保存图片
          },
        },
        xAxis: {
          // data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
          type: "category",
          boundaryGap: false,
          data: list.map((item) => item.x),
        },
        yAxis: { type: "value" },
        series: [
          {
            name: "销量a",
            type: "bar",
            data: [5, 20, 36, 10, 10, 20],
            markPoint: {
              data: [
                { type: "max", name: "Max" },
                { type: "min", name: "Min" },
              ],
            },
            markLine: {
              data: [{ type: "average", name: "Avg" }],
            },
          },
          {
            name: "销量b",
            type: "line",
            data: list.map((item) => item.val),
            markPoint: {
              data: [
                { type: "max", name: "Max" },
                { type: "min", name: "Min" },
              ],
            },
            markLine: {
              data: [
                { type: "average", name: "Avg" },
                [
                  {
                    symbol: "none",
                    x: "90%",
                    yAxis: "max",
                  },
                  {
                    symbol: "circle",
                    label: {
                      position: "start",
                      formatter: "Max",
                    },
                    type: "max",
                    name: "最高点",
                  },
                ],
              ],
            },
          },
          {
            name: "Nightingale Chart",
            type: "pie",
            radius: [50, 250],
            center: ["50%", "50%"],
            roseType: "area",
            itemStyle: {
              borderRadius: 8,
            },
            data: list.map((item) => ({ value: item.val, name: item.x })),
          },
        ],
      });
    },
  },
};
</script>
<style scoped></style>
